package com.example.springbootdemo.api

import com.example.springbootdemo.entity.Person
import com.example.springbootdemo.repository.PersonRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/people")
class PersonApi(
        val personRepository: PersonRepository
) {
    @GetMapping("")
    fun getAll() = personRepository.findAll()

    @GetMapping("/{id}")
    fun get(@PathVariable id: Long) = personRepository.findByIdOrNull(id)

    @GetMapping("/addTest")
    fun addTest() = personRepository.saveAll(
            listOf(
                    Person(1, "林yz"),
                    Person(2, "丘qm"),
                    Person(3, "梁rj"),
                    Person(4, "梁wl")
            )
    )
}